import axios from 'axios'
import { ElMessage } from 'element-plus'
import { getStorage } from './storage'
import { basicToken } from './auth'

export const BASE_URL = 'https://it.zeng.pub/lvyou/apif'

const request = axios.create({
  baseURL: BASE_URL,
  // baseURL: 'http://localhost:8900/apif',
  timeout: 10000
})

// 配置请求拦截器
request.interceptors.request.use(
  (config) => {
    // 在请求头中 携带token值 [后端采用basicAuth模式做token解密]
    const { user_Token } = getStorage('adminUser')

    config.headers.Authorization = basicToken(user_Token)

    return config
  },
  (error) => Promise.reject(error)
)

// 配置响应拦截器
request.interceptors.response.use(
  (res) => {
    // 返回响应结果
    return res.data
  },
  (error) => {
    const { data } = error.response
    ElMessage.error(data.msg || '请求失败')
    return Promise.reject(error)
  }
)

export default request
